package org.example.ACM;

import java.util.HashMap;

/**
 * 两数之和
 */
public class noob120 {
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int tar;
        for (int i = 0; i < nums.length; i++) {
            tar = target - nums[i];
            if (map.containsKey(tar)) {
                if (i > map.get(tar)) {
                    return new int[]{map.get(tar) + 1, i + 1};
                } else {
                    return new int[]{i + 1, map.get(tar) + 1};
                }
            }
            map.put(nums[i], i);
        }
        return new int[]{};
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        int[] result = twoSum(nums, target);
        System.out.println(result[0] + " " + result[1]);
    }
}
